package com.lipeng.controller;

import com.lipeng.bean.Admin;
import com.lipeng.service.AdminService;
import com.lipeng.utils.AjaxResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by dllo on 18/3/1.
 */
@Controller
public class AdminController {
    @Resource
    private AdminService adminService;

    @RequestMapping(value = "adminLogin")
    public String adminLogin(){
        return "login";
    }

    @ResponseBody
    @RequestMapping(value = "dologin")
    public AjaxResult dologin(@Param("name") String name, @Param("psw") String pwd,  HttpServletRequest request) {
        Admin admin = new Admin(name, pwd);
        AjaxResult result = adminService.adminLogin(admin);
        if (result.getData() != null){
            Admin newAdmin = (Admin) result.getData();
            request.getSession().setAttribute("Id",newAdmin.getId());
            request.getSession().setAttribute("Name",newAdmin.getName());
        }
        return result;
    }
}
